function getSys(type) {
  let sys
  if (type == 'localStorage') {
    sys = localStorage
  } else {
    sys = sessionStorage
  }
  return sys
}
export default {
  getItem(name, type) {
    let sys = getSys(type)
    if (typeof name !== 'string') {
      return
    }
    var savedStr = sys.getItem(name)
    // 非法值返回，包括undefined、null、空字符串
    if (!savedStr) {
      return
    }
    var result
    if (
      (savedStr.indexOf('"') < 0 && savedStr.indexOf("'") < 0) ||
      savedStr.indexOf(':') < 0
    ) {
      return savedStr
    }
    try {
      result = JSON.parse(savedStr)
      return result
    } catch (e) {
      return savedStr
    }
  },
  setItem(name, obj, type) {
    let sys = getSys(type)
    if (typeof obj === 'undefined') {
      return false
    }
    // obj为undefined或null或空字符串不能存储，布尔值可存储，但取时为字符串
    if (
      !obj &&
      (typeof obj === 'undefined' ||
        typeof obj === 'object' ||
        typeof obj === 'string')
    ) {
      return false
    }
    let saveStr = ''
    if (typeof obj === 'object') {
      saveStr = JSON.stringify(obj)
    } else {
      saveStr = obj
    }
    sys.setItem(name, saveStr)
    return true
  },
  removeItem(name, type) {
    let sys = getSys(type)

    if (typeof name !== 'string') {
      return
    }
    sys.removeItem(name)
  },
  clearAll(name, type) {
    let sys = getSys(type)
    if (name) {
      return
    }
    sys.clear()
  }
}
